const CACHE_CONFIG = {
  EXPRIE_TIME: 7 * 24 * 60 * 60 * 1000,
  VERSION: process.env.VUE_APP_STORAGE_VERSION == null ? '0.0' : process.env.VUE_APP_STORAGE_VERSION
};
const CACHE_KEY = {};
const default_cache = {
  version: CACHE_CONFIG.VERSION,
  date: Date.now()
};

/**
 * 加密json数据
 * @param {object | string} aJson 加密数据
 */
export function encryptionJson (aJson) {
  const data = JSON.stringify(aJson);
  let str = '';
  const alterText = [];
  const varCost = [];
  const TextLength = data.length;
  for (let i = 0; i < TextLength; i++) {
    const random = parseInt(Math.random() * 266);
    alterText[i] = data.charCodeAt(i) + random;
    varCost[i] = random;
  }
  for (let i = 0; i < TextLength; i++) {
    str += String.fromCharCode(alterText[i], varCost[i]);
  }
  return str;
}
/**
 * 解密json数据
 * @param {string} text 解密字符串
 */
export function decrypt (text) {
  let str = '';
  const alterText = [];
  const varCost = [];
  const TextLength = text.length;
  for (let i = 0; i < TextLength; i++) {
    alterText[i] = text.charCodeAt(i);
    varCost[i] = text.charCodeAt(i + 1);
  }
  for (let i = 0; i < TextLength; i = i + 2) {
    str += String.fromCharCode(alterText[i] - varCost[i]);
  }
  return JSON.parse(str);
}

const cache = {
  default_cache,
  CACHE_KEY,
  CACHE_CONFIG,
  init: key => {
    const nowCache = cache.getFromStorage(key);
    if (cache.isSameVersion(nowCache) && !cache.isExprie(nowCache)) {
      cache.default_cache = nowCache;
    }
    cache.setIntoStorage(key, cache.default_cache);
  },
  get: key => {
    const nowCache = cache.getFromStorage(key);
    if (cache.isSameVersion(nowCache) && !cache.isExprie(nowCache)) {
      return nowCache[key];
    }
    return null;
  },
  set: (key, value) => {
    let nowCache = cache.getFromStorage(key);
    if (!nowCache) {
      nowCache = cache.default_cache;
    }
    nowCache.date = Date.now();
    nowCache.version = cache.CACHE_CONFIG.VERSION;
    nowCache[key] = value;
    cache.setIntoStorage(key, nowCache);
  },
  setIntoStorage: (key, value) => {
    try {
      // const saveData = value ? encryptionJson(value) : null;
      const saveData = value ? JSON.stringify(value) : null;
      localStorage.setItem(key, saveData);
    } catch (error) {
      console.error(error);
      localStorage.setItem(key, null);
    }
  },
  getFromStorage: key => {
    try {
      const str = localStorage.getItem(key);
      // const backData = str ? decrypt(str) : null;
      const backData = str ? JSON.parse(str) : null;
      return backData;
    } catch (error) {
      console.error(error);
      return null;
    }
  },
  isSameVersion: nowCache => {
    if (!nowCache) {
      return true;
    }
    const nowVersion = nowCache.version;
    return nowVersion && nowVersion === cache.CACHE_CONFIG.VERSION;
  },
  isExprie: nowCache => {
    if (!nowCache) {
      return true;
    }
    const nowDateTime = nowCache.date;
    return nowDateTime && Date.now() - nowDateTime > cache.CACHE_CONFIG.EXPRIE_TIME;
  },
  clear: key => {
    console.log(key);
    if (key) {
      localStorage.removeItem(key);
    } else {
      localStorage.clear();
    }
  }
};
export default cache;
